//
//  YPRequestViewModel.swift
//  cxd4iphone
//
//  Created by hexy on 12/18/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit
import ReactiveCocoa

class YPRequestViewModel: NSObject {
    
    static let shared = YPRequestViewModel()
    
    override init() {
        
        ypRequestModel = YPRequestModel(dict: ["":""])
   
    }

    var ypRequestModel: YPRequestModel?
    
    var html: String {
        
        return xx_HTML((ypRequestModel?.actionUrl)!, sign: (ypRequestModel?.sign)!, req: (ypRequestModel?.reqContent)!)
    }
    
    var callBackURL: String {
        
        return ypRequestModel?.callbackUrl ?? ""
    }
    
   
    func recharge(money: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            
            APIManager.recharge(money).subscribeNext({ (result) -> Void in
                
                //                                xx_JsonPrint(dict: result)
                let dict = result as! [String: AnyObject]
                
                self.ypRequestModel = YPRequestModel(dict: dict)
                
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })

            return RACDisposable(block: {})
        })
    }
    
    func withdraw(money: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            
            APIManager.withdraw(money).subscribeNext({ (result) -> Void in
                
                //                                xx_JsonPrint(dict: result)
                let dict = result as! [String: AnyObject]
                
                self.ypRequestModel = YPRequestModel(dict: dict)
                
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            
            return RACDisposable(block: {})
        })
    }


    
    func invest(loanId: String, investMoney: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            APIManager.invest(loanId, investMoney: investMoney).subscribeNext({ (result) -> Void in
                
                //                                xx_JsonPrint(dict: result)
                    let dict = result as! [String: AnyObject]
                    
                    self.ypRequestModel = YPRequestModel(dict: dict)
                    
                    subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            
            return RACDisposable(block: {})
        })
    }
    
    func bindBankCard() -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            APIManager.bindBankCard().subscribeNext({ (result) -> Void in
                
                //                                xx_JsonPrint(dict: result)
                let dict = result as! [String: AnyObject]
                
                self.ypRequestModel = YPRequestModel(dict: dict)
                
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })

            return RACDisposable(block: {})
        })
    }
    
    func authentication(realName: String, idCard: String) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            APIManager.authentication(realName, idCard: idCard).subscribeNext({ (result) -> Void in
                
                                                xx_JsonPrint(dict: result)
                let dict = result as! [String: AnyObject]
                
                self.ypRequestModel = YPRequestModel(dict: dict)
                
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })

            
            return RACDisposable(block: {})
        })
    }
    
    func changeTradePwd() -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            APIManager.changeTradePwd().subscribeNext({ (result) -> Void in
                
                xx_JsonPrint(dict: result)
                let dict = result as! [String: AnyObject]
                
                self.ypRequestModel = YPRequestModel(dict: dict)
                
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            
            
            return RACDisposable(block: {})
        })
    }
    
    
    func changeMobile() -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
            
            
            APIManager.changeMobile().subscribeNext({ (result) -> Void in
                
                xx_JsonPrint(dict: result)
                let dict = result as! [String: AnyObject]
                
                self.ypRequestModel = YPRequestModel(dict: dict)
                
                subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            
            
            return RACDisposable(block: {})
        })
    }

    
}
